﻿using UnityEngine.Events;
using TMPro;
using static CyberU;
using static CyberU.UI;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;

public class SimpleText : RectComponent
{
    public UnityEvent<bool> onEmptyStateChanged= new UnityEvent<bool>();

    public void AddEmptyListener(UnityAction<bool> callback)
    {
        onEmptyStateChanged.AddListener(callback);
    }

    public void RemoveEmptyListener(UnityAction<bool> callback)
    {
        onEmptyStateChanged.RemoveListener(callback);
    }

    public TextMeshProUGUI textComponent;

    public UnityEvent<string, string> onTextChanged = new UnityEvent<string, string>();


    public void Awake()
    {
        if (textComponent == null)
        {
            textComponent = AddOrGetComponent<TextMeshProUGUI>(gameObject);
        }

        onTextChanged.AddListener((oldString, newString) =>
        {
            {
                // Delogb();
                bool wasEmpty = string.IsNullOrEmpty(oldString);
                bool isEmpty = string.IsNullOrEmpty(newString);
                // Deloga(newString,isEmpty,onEmptyStateChanged!=null);
                if (wasEmpty != isEmpty){
                    onEmptyStateChanged.Invoke(isEmpty);}
            }
        });
    }

    public event UnityAction<float, float> onFontSizeChanged;
    public TextInput textInput;
    public TMP_InputField inputField;

    public string text
    {
        get
        {
            if (Used(textInput))
            {
                return ClarifiedStr(inputField.text);
            }

            if (textComponent == null)
            {
                WarnLog("the textComponent of simpleText of {gameObject.name} is null!!!");
            }

            // Delogf(ScenePath(gameObject));
            // Delogf(ScenePath(gameObject),ScenePath(textComponent),textComponent==null);
            return ClarifiedStr(textComponent.text);
        }
        set
        {
            if (text != value)
            {
                onTextChanged.Invoke(text, value);
            }
            if (Used(textInput))
            {
                // Delog($"set with direct text {value}");
                inputField.text = value;
            }
            else
            {
                textComponent.text = value;
            }
            // StartCoroutine(WaitAFrame());
            // Delogb(text,isEmpty);
            // Delogc(value,textComponent.text);
        }
    }

    public float fontSize
    {
        get => textComponent.fontSize;
        set
        {
            float oldSize = textComponent.fontSize;
            textComponent.fontSize = value;
            onFontSizeChanged?.Invoke(oldSize, value);
        }
    }

    public void AddTextChangedListener(UnityAction<string, string> callback)
    {
        onTextChanged.AddListener(callback);
    }

    public void RemoveTextChangedListener(UnityAction<string, string> callback)
    {
        onTextChanged.RemoveListener(callback);
    }

    public void AddFontSizeChangedListener(UnityAction<float, float> callback)
    {
        onFontSizeChanged += callback;
    }

    public void RemoveFontSizeChangedListener(UnityAction<float, float> callback)
    {
        onFontSizeChanged -= callback;
    }

    public Color color
    {
        get { return textComponent.color; }
        set { textComponent.color = value; }
    }

    public string name
    {
        get { return this.text; }
        set { this.text = value; }
    }


    public TMPro.TMP_FontAsset font
    {
        get { return textComponent.font; }
        set { textComponent.font = value; }
    }
}